﻿using JZ_UDI.DataAccess;
using JZ_UDI.DataAccess.DataEntity;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JZ_UDI.View.Settings
{
    public partial class frmVendorCode : Form
    {
        public frmVendorCode()
        {
            InitializeComponent();
        }

        List<VendorInfo> lists;
        frmVendorCodeAdd fvc;
        private void frmVendorCode_Load(object sender, EventArgs e)
        {
            using (var context = new EFDbContext())
            {
                var sql = "select A.Vendor_Code,A.ActivatedState,isnull(B.Quantity,0) as Quantity,isnull(B.MaxSerialNumber,0) as MaxSerialNumber  from VendorCode A left join (select VendorCode, count(*) as Quantity, max(SerialNumber) as MaxSerialNumber from Product where isDeleted = 0 group by VendorCode) B on A.Vendor_Code = B.VendorCode where A.isDeleted = 0";
                lists = context.Database.SqlQuery<VendorInfo>(sql).ToList();
            }
            dgv.AutoGenerateColumns = Focused;
            dgv.DataSource = lists;
            dgv.Columns[0].DataPropertyName = "Vendor_Code";
            dgv.Columns[1].DataPropertyName = "ActivatedState";
            dgv.Columns[2].DataPropertyName = "Quantity";
            dgv.Columns[3].DataPropertyName = "MaxSerialNumber";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            fvc = new frmVendorCodeAdd();
            fvc.addFormClose += Fvc_addFormClose;
            fvc.ShowDialog();
        }

        private void Fvc_addFormClose(object sender, string result)
        {

            frmVendorCode_Load(null, null);

            fvc.addFormClose -= Fvc_addFormClose;
            fvc.Dispose();
            fvc = null;
        }

        private void btnActive_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count <= 0)
            {
                return;
            }
            VendorInfo vi = dgv.SelectedRows[0].DataBoundItem as VendorInfo;
            using (var context = new EFDbContext())
            {
                List<VendorCode> lvcs = context.VendorCodes.ToList();
                foreach (VendorCode oldVi in lvcs)
                {
                    if (oldVi.Vendor_Code.Equals(vi.Vendor_Code))
                    {
                        oldVi.ActivatedState = "是";
                        Global.VendorCode = oldVi.Vendor_Code;   //更新全局厂商代码
                        Global.SaveLogger(LogLevel.Info, Global.logger.Name,oldVi.Vendor_Code+ "被激活");
                    }
                    else
                    {
                        oldVi.ActivatedState = "否";
                    }
                }
                int count = context.SaveChanges();
                if (count > 0)
                {
                    frmVendorCode_Load(null, null);
                }
                else
                {
                    MessageBox.Show("激活失败!");
                }
            }
        }

        private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            // 获取激活状态的行，改变字体颜色  
            DataGridViewRow row = dgv.Rows[e.RowIndex];

            if (row.Cells[1].Value.Equals("是"))
            {
                row.DefaultCellStyle.ForeColor = Color.Red;
            }
            else
            {
                row.DefaultCellStyle.ForeColor = Color.Black;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count <= 0)
            {
                return;
            }
            VendorInfo vcs = dgv.SelectedRows[0].DataBoundItem as VendorInfo;
            if (vcs.ActivatedState.Equals("是"))
            {
                MessageBox.Show("若要删除激活数据，请先激活另一条数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }




            if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }



            using (var context = new EFDbContext())
            {
                VendorCode _vc = context.VendorCodes.ToList().Find(o => o.Vendor_Code == vcs.Vendor_Code);
                _vc.isDeleted = 1;
                int count = context.SaveChanges();
                if (count <= 0)
                {
                    Global.SaveLogger(LogLevel.Info, Global.logger.Name, "删除失败，厂商代码：" + _vc.Vendor_Code);
                    MessageBox.Show("删除失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    Global.SaveLogger(LogLevel.Info, Global.logger.Name, "删除"+_vc.Vendor_Code+"成功");
                    frmVendorCode_Load(null, null);
                }
            }

        }
    }




    public class VendorInfo
    {
        /// <summary>
        /// 厂商代码
        /// </summary>
        public string Vendor_Code { get; set; }
        /// <summary>
        /// 激活状态 
        /// </summary>
        public string ActivatedState { get; set; }

        /// <summary>
        /// 已用数量
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// 最大序号
        /// </summary>
        public string MaxSerialNumber { get; set; }
    }

}
